<?php
namespace ChannelThrift\Service;

use Base\BaseService;

class DealerBailLogService extends BaseService {
    Protected $autoCheckFields = false;


    /**
     * 获取单个车商保证金日志
     * @param integer $dealerId
     * @return array
     */
    public function getDealerBailLog($dealerId, $orderID = 0, $useType = 0, $isFree = null)
    {
        if (empty($dealerId)) {
            return false;
        }
        $map = array();
        $map['dealer_id'] = $dealerId;
        if (!empty($orderID)) {
            $map['order_id'] = $orderID;
        }
        if (!empty($useType)) {
            $map['use_type'] = $useType;
        }
        if ($isFree !== null) {
            $map['is_free'] = $isFree;
        }
        return D('ChannelThrift/Aums/DealerBailLog')->where($map)->find();
    }

    /**
     * 获取单个车商保证金日志多条
     * @param integer $dealerId
     * @param integer $orderID
     * @return array
     */
    public function getDealerBailLogList($dealerId, $orderID = 0)
    {
        if (empty($dealerId)) {
            return false;
        }
        $map = array();
        $map['dealer_id'] = $dealerId;
        $map['use_type'] = 2;
        $map['is_free'] = 0;

        if (!empty($orderID)) {
            $map['order_id'] = $orderID;
        }
        return D('ChannelThrift/Aums/DealerBailLog')->field('bl_id, dealer_id, occur_money')->where($map)->select();
    }

    /**
     * 插入保证金使用记录
     * @param int $dealerId 车商ID
     * @param int $orderID 拍单ID
     * @param int $useType 保证金应用类型 冻结出价保证金
     * @param string $useDetail 应用明细
     * @param int $occurMoney 发生金额
     * @param int $remainAmount 余额
     * @param int $rechargeMethod
     * @param int $freeBlId
     * @param int $payMethodId
    return bool|mixed
     */
    public function insertDealerBailLog($dealerId = 0, $orderID = 0, $useType = 0, $useDetail = '', $occurMoney = 0, $remainAmount = 0, $rechargeNo = '', $rechargeId = 0 ,$rechargeMethod = 0, $freeBlId = 0 ,$collectionId = 0,$payMethodId=0)
    {
        if (empty($dealerId) ) {
            return false;
        }
        $now = date("Y-m-d H:i:s", time());
        $data = array(
            'dealer_id' => $dealerId,               //int(10)车商id
            'order_id'  => intval($orderID),         //int(10)拍单号
            'use_time'  => $now,                     //保证金使用时间
            'use_type'  => $useType,                 //smallint(3)保证金应用类型 充值
            'use_detail'  => $useDetail,             //text应用明细
            'occur_money' => $occurMoney,           //double(10,2)发生金额
            'remain_amount' => $remainAmount,       //double(10,2)余额
            'recharge_method' => $rechargeMethod,   //smallint(3)充值方式
            'recharge_no' =>$rechargeNo,             //充值单号
            'createtime'  => $now,                   //datetime创建时间
            'free_bl_id'  => $freeBlId,              //int(11)解冻记录ID
            'recharge_id' =>intval($rechargeId),             //int(11)订单表ID
            'collection_id' =>intval($collectionId),         //int(11)收款单ID
            'payment_id' => intval($payMethodId)            //int(11)付款单ID
        );
        return D('ChannelThrift/Aums/DealerBailLog')->add($data);
    }

    /**
     * 标记是否解冻
     * 若为冻结记录时，标识是否已解冻 ，1解冻，0未解冻_
     * @param integer $blId
     * @return bool
     */
    public function setIsFree($blId)
    {
        if (empty($blId) ) {
            return false;
        }
        return D('ChannelThrift/Aums/DealerBailLog')->where('bl_id = ' . $blId)->save( array('is_free' => 1) );
    }

    /** 查询保证金使用记录
     * @param $where
     * @param int $page
     * @param int $pageSize
     * @return mixed
     */
    public function selectDealerLog($where,$page=1,$pageSize=20){
        $log  = D("ChannelThrift/Aums/DealerBailLog")
            ->field("bl_id,dealer_id,order_id,use_time,use_type,use_detail,occur_money,remain_amount,recharge_method,createtime,is_free")
            ->where($where)
            ->order('bl_id desc,createtime desc')
            ->limit($pageSize*($page-1),$pageSize)
            ->select();
        return $log;
    }

    /**分页数量
     * @param $where
     * @return mixed
     */
    public function countDealerLog($where){
        $nums = D("ChannelThrift/Aums/DealerBailLog")->field("count(1) as nums")->where($where)->find();
        return $nums;
    }

    /** 获取未拍卖成功
     * @param $orderId
     * @param $dealerID
     * @return mixed
     */
    public function getUnFreeDealers($orderId,$dealerID){
        $dealers  = D("ChannelThrift/Aums/DealerBailLog")
            ->field("bl_id,dealer_id,occur_money")
            ->where(" is_free=0 and use_type=2 and  order_id='".$orderId."' and dealer_id !='".$dealerID."'")
            ->select();
        return $dealers;
    }


}
